import asyncio
from tap_titans.providers import providers
from tap_titans.models import models

AUTH_TOKEN = "e97b51bb-7ed4-4d39-b826-1a2c2f0d9e76"
PLAYER_TOKENS = ["4ab6d84a-fe23-4f14-9d7c-e49a5435e8c3"]


async def connected():
    print("Connected")

    r = providers.RestAPI(AUTH_TOKEN)

    resp = await r.subscribe(PLAYER_TOKENS)
    if len(resp.refused) > 0:
        print("Failed to subscribe to clan with reason:",
              resp.refused[0].reason)
    else:
        print("Subscribed to clan:", resp.ok[0].clan_code)


async def clan_added_cycle(message: models.raid_models.RaidCycleResetClanAdded):
    print("Clan Added Cycle", message)
    print('===============')
    print("Raid level of clan", message.raid.level)
    print('===============')


async def raid_attack_end(message: models.raid_models.RaidAttackEnd):
    print("Raid Attack", message)
    print('===============')


async def raid_attack_start(message: models.raid_models.RaidAttackStart):
    print("Raid Attack Start", message)
    print('===============')

async def player_connected():
    print("Player WS is connected, can now use the rest API /player endpoints")
    r = providers.RestAPI(AUTH_TOKEN)

    # resp = await r.player_data(PLAYER_TOKENS[0], providers.PlayerDataProperties.all())

    # print("Name of the player who owns the token:", resp.name)

websocket = providers.WebsocketClient(
    connected=connected,
    raid_attack_end=raid_attack_end,
    raid_attack_start=raid_attack_start,
    clan_added_cycle=clan_added_cycle,
    setting_validate_arguments=True,
    player_connected=player_connected,
)

asyncio.run(websocket.connect(AUTH_TOKEN))
